﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAC.Elink;
using SAC.sqlHelper;
using System.Data;

namespace Bussiness
{
    /// <summary>
    /// 报告模板类
    /// </summary>
    public class ThermalTestTemplate
    {
        string errMsg = "";

        /// <summary>
        /// 热力试验模板标识号
        /// </summary>
        public int ID_KEY { get; set; }
        /// <summary>
        /// 模板标识号
        /// </summary>
        public string TemplateName { get; set; }
        /// <summary>
        /// 机组编号
        /// </summary>
        public string UnitID { get; set; }
        /// <summary>
        /// Para表ParaID
        /// </summary>
        public string ParaID { get; set; }
        /// <summary>
        /// 显示序列
        /// </summary>
        public int SN { get; set; }
        /// <summary>
        /// 版本
        /// </summary> 
        public int Version { get; set; }
        /// <summary>
        /// 模版编号
        /// </summary>
        public int TemplateID { get; set; }
        /// <summary>
        /// 插入实验模版 
        /// </summary>
        public void InsertTemplate()
        {
            string sql = "INSERT INTO ThermalTestTemplate(TEMPLATENAME,UNITID,PARAID,SN,TEMPLATEID) VALUES('" + TemplateName + "','" + UnitID + "','" + ParaID + "'," + SN + "," + TemplateID + ")";
            //DB2Provider.DataLink link = new DB2Provider.DataLink();
            //link.Excute(sql);
            DBsql.RunNonQuery(sql, out errMsg);
        }
        /// <summary>
        /// 获取所有不同实验名
        /// </summary>
        /// <returns></returns>
        public DataTable GetReportName()
        {

            DataTable dt = null;
            try
            {
                string sql = "";
                if (UnitID == "0")
                    //sql = "SELECT DISTINCT(TemplateName),TemplateID FROM ThermalTestTemplate";
                    sql = "SELECT DISTINCT(TemplateName) FROM ThermalTestTemplate";
                else
                    sql = "SELECT DISTINCT(TemplateName) FROM ThermalTestTemplate WHERE UNITID='" + UnitID + "'";
                //DB2Provider.DataLink link = new DB2Provider.DataLink();
                dt = DBsql.RunDataTable(sql, out errMsg);// link.ExcuteRetureTable(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        /// <summary>
        /// 返回报告模版编号
        /// </summary>
        /// <param name="templateName">实验名</param>
        /// <param name="unitId">机组编号</param>
        /// <returns></returns>
        public int GetTemplateID(string templateName, string unitId)
        {
            DataTable dt = null;
            try
            {

                string sql = "SELECT TemplateID FROM ThermalTestTemplate WHERE TemplateName='" + templateName + "' AND UNITID='" + unitId + "'";
                if (unitId == "0")
                {
                    if (templateName != "0")
                        sql = "SELECT TemplateID FROM ThermalTestTemplate WHERE TemplateName='" + templateName + "'";
                    else
                        return 0;
                }
                //else
                //{
                //    if (templateName == "0")
                //        sql = "SELECT TemplateID FROM ThermalTestTemplate WHERE TemplateName='" + templateName + "'";
                //}
                //DB2Provider.DataLink link = new DB2Provider.DataLink();
                dt = DBsql.RunDataTable(sql, out errMsg);// link.ExcuteRetureTable(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return Convert.ToInt32(dt.Rows[0][0]);
        }
        /// <summary>
        /// 返回报告模版编号集
        /// </summary>
        /// <param name="templateName"></param>
        /// <param name="unitId"></param>
        /// <returns></returns>
        public DataTable GetTemplateIDs(string templateName, string unitId)
        {
            DataTable dt = null;
            try
            {

                string sql = "SELECT TemplateID FROM ThermalTestTemplate WHERE TemplateName='" + templateName + "' AND UNITID='" + unitId + "'";
                if (unitId == "0")
                {
                    if (templateName != "0")
                        sql = "SELECT TemplateID FROM ThermalTestTemplate WHERE TemplateName='" + templateName + "'";
                }
                // DB2Provider.DataLink link = new DB2Provider.DataLink();
                dt = DBsql.RunDataTable(sql, out errMsg); //link.ExcuteRetureTable(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        /// <summary>
        /// 获取实验报告模版
        /// </summary>
        /// <returns></returns>
        public DataTable GetReportByTempleteID()
        {
            DataTable dt = null;
            try
            {
                string sql = "SELECT * FROM ThermalTestTemplate WHERE ID_KEY=" + ID_KEY + "";
                //DB2Provider.DataLink link = new DB2Provider.DataLink();
                dt = DBsql.RunDataTable(sql, out errMsg);// link.ExcuteRetureTable(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        /// <summary>
        /// 判断实验模版参数是否存在
        /// </summary>
        /// <param name="templateId">模版编号</param>
        /// <param name="paraId">参数编号</param>
        /// <returns></returns>
        public bool IsExitTemplatePara(int templateId, string paraId)
        {
            bool flag = false;
            string sql = "SELECT count(*) FROM ThermalTestTemplate WHERE PARAID='" + paraId + "' AND TemplateID=" + templateId;
            //DB2Provider.DataLink link = new DB2Provider.DataLink();
            DataTable dt = DBsql.RunDataTable(sql, out errMsg);// link.ExcuteRetureTable(sql);
            if (dt != null && dt.Rows.Count > 0)
                if (dt.Rows[0][0].ToString() != "0")
                    flag = true;

            return flag;
        }

    }
}
